#! /usr/bin/python3

import pymysql

# 打开数据库连接
db = pymysql.connect('localhost', 'root', '123456', 'testdb')

# 使用 cursor() 方法创建一个游标对象cursor
cursor = db.cursor()

# SQL查询语句
sql = 'select * from employee \
      where income > {0:d}'.format(1000)
try:
    # 执行SQL语句
    cursor.execute(sql)
    # 获取所有记录列表
    results = cursor.fetchall()
    for row in results:
       table = {}
       table['fname'] = row[1]
       table['lname'] = row[2]
       table['age'] = row[3]
       table['sex'] = row[4]
       table['income'] = row[5]
       print('''fname={fname:s}, lname={lname:s},  
              sex={sex}, age={age}, income={income}'''.format(**table))
except:
    print("Error: unable to fetch data")
# 关闭数据库连接
db.close()
